#include "videoplayerdialog.h"
#include "models/command.h"
#include <QKeyEvent>
#include <QFileInfo>
#include <QStyle>
#include <QDebug>
#include <QFile>
#ifdef Q_OS_ANDROID
#include <QMediaPlayer>
#include <QVideoWidget>
#else
#include <QWebEngineSettings>
#include <QWebEnginePage>
#include <QWebEngineFullScreenRequest>
#endif

VideoPlayerDialog::VideoPlayerDialog(const QString& videoPath, QWidget* parent)
    : PopupDialog(parent)
{
    setGeometry(parentWidget()->geometry());
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(int(20*g_xScale), int(20*g_yScale), int(20*g_xScale), int(30*g_yScale));
    layout->setSpacing(0);

    // 创建顶部工具栏
    QWidget* topBar = new QWidget(this);
    topBar->setStyleSheet("background-color: transparent;");
    QHBoxLayout* topLayout = new QHBoxLayout(topBar);

    // 创建图片名称标签
    QFileInfo fileInfo(videoPath);
    QLabel* nameLabel = new QLabel(fileInfo.fileName(), this);
    nameLabel->setStyleSheet(QString("color: white; font-family: '微软雅黑'; font-size: %1px;").arg(int(20*g_xScale)));

    // 创建关闭按钮
    m_closeButton = new TouchButton(this);
    m_closeButton->setFixedSize(int(40*g_xScale), int(40*g_xScale));
    m_closeButton->setStyleSheet(
        "QPushButton{ \
            border-image: url(:/images/close.png); \
            background-color:transparent; \
        } \
        QPushButton:pressed{ \
            background-color: rgb(255, 0, 0); \
        }"
    );

    topLayout->setContentsMargins(0, 0, 0, 0);
    topLayout->setSpacing(0);
    topLayout->addWidget(nameLabel);
    topLayout->addStretch();
    topLayout->addWidget(m_closeButton);

#ifdef Q_OS_ANDROID
    // 安卓平台使用 QVideoWidget
    m_videoWidget = new QVideoWidget(this);
    m_mediaPlayer = new QMediaPlayer(this);
    m_mediaPlayer->setVideoOutput(m_videoWidget);
    m_mediaPlayer->setMedia(QUrl::fromLocalFile(videoPath));
    
    layout->addWidget(topBar);
    layout->addWidget(m_videoWidget, 1);
    
    // 设置背景色
    m_videoWidget->setStyleSheet("background-color: black;");
    
    // 自动播放
    m_mediaPlayer->play();
#else
    // Windows 平台使用 QWebEngineView
    m_webView = new QWebEngineView(this);
    QWebEngineSettings *settings = m_webView->settings();
    settings->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, false);
    settings->setAttribute(QWebEngineSettings::PluginsEnabled, true);
    settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true);

    layout->addWidget(topBar);
    layout->addWidget(m_webView, 1);

    connect(m_webView, &QWebEngineView::loadFinished, this, &VideoPlayerDialog::onLoadFinished);

    // 读取 HTML 模板
    QFile file(":/view/controlpage/setting/mediamanage/videoplayer.html");
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Failed to open videoplayer.html";
        return;
    }

    QString htmlTemplate = file.readAll();
    file.close();

    QString html = htmlTemplate.arg(videoPath);
    m_webView->setHtml(html, videoPath);
    m_webView->setStyleSheet("background-color: black;");

    if (auto page = m_webView->page()) {
        page->setBackgroundColor(Qt::black);
    }
#endif

    connect(m_closeButton, &QPushButton::clicked, this, &QDialog::accept);
}

VideoPlayerDialog::~VideoPlayerDialog()
{
#ifdef Q_OS_ANDROID
    if (m_mediaPlayer) {
        m_mediaPlayer->stop();
    }
#endif
}

void VideoPlayerDialog::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape) {
        accept();
    }
    QDialog::keyPressEvent(event);
}

void VideoPlayerDialog::onLoadFinished(bool ok)
{
    if (ok) {
        m_webView->page()->runJavaScript(
            "window.videoPlayer.play()",
            [](const QVariant &result) {
                Q_UNUSED(result);
            }
        );
    } else {
        qDebug() << "页面加载失败";
    }
}
